<?php
/*
 * Camelot PHP Tools
 *
 * @version 2.0
 * @author Bendsoft
 * @package PHP Tools
 * @subpackage Camelot
 * @license FreeBSD License (www.bendsoft.com/licensing/)
 *
 * Note! 
 * Set up settings.php before using this file
 * 
 * Example contact form PHP to SharePoint
 * http://blog.bendsoft.com/2011/04/php-to-sharepoint-contact-us-form/
 *
 */

header('Content-Type: text/html; charset=utf-8'); // ensure to send data in a compatible format
error_reporting(E_ALL); // Ensure all errors are thrown to the browser
ini_set('display_errors', '1');

// Load all required files for Camelot PHP Tools
require_once '../loader.php';

/*
 * Recieve incomming form post
 */
if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST') {

    try {
        // Build the SQL command
        $name = addslashes($_POST['name']);
        $email = addslashes($_POST['email']);
        $company = addslashes($_POST['company']);
        $message = addslashes($_POST['message']);


        // Execute the query
        $SharePointNonQuery = new SharePointNonQuery(array(
            'sql' => "INSERT INTO ContactForm (title,email,company,message) VALUES ('$name','$email','$company','$message')",
            'method' => 'ExecuteNonQuery',
            'connection_name' => 'SharePointConnection2'
        ));

        // On success
        if ($SharePointNonQuery->_result) {
            echo("<script>alert('Your message has been sent to our team. We will handle your enquiry as soon as possible!');</script>");
        } else {
            echo("<script>alert('We could not recieve your message at the moment, please try again or send your message to info@example.com');</script>");
        }
        
    } catch (Exception $exc) {
        echo $exc->getTraceAsString();
    }
}
?>
<html>
    <head>
        <title>Contact Us</title>
    </head>

    <style type="text/css">
        body {font-family: 'Segoe UI','Lucida Grande',Helvetica,'Helvetica Neue',Verdana,sans-serif;}
        td {vertical-align: top;}
        td em {font-size: 80%;}
        input, textarea {border: 1px solid #ccc; margin: 2px; padding: 5px;}
        input#submit {background-color: #EBF3FF;}
    </style>

    <script language="javascript" type="text/javascript">
        /**
         * Very simple validate method
         */
        function validateForm()
        {
            // check name
            var name=document.forms["contactform"]["name"].value
            if (name==null || name=="")
            {
                alert("Name must be filled out");
                return false;
            }

            // check email
            var email=document.forms["contactform"]["email"].value
            var atpos=email.indexOf("@");
            var dotpos=email.lastIndexOf(".");
            if (atpos<1 || dotpos<atpos+2 || dotpos+2>=email.length)
            {
                alert("Not a valid e-mail address");
                return false;
            }

            // check message
            var message=document.forms["contactform"]["message"].value
            if (message==null || message=="" || message.length < 5)
            {
                alert("There must be a message");
                return false;
            }
        }
    </script>

    <body>

        <h2>Contact Us</h2>
        <p>Please use the form below to contact us. We will get back to you as quick as possible!</p>
        <form method="post" name="contactform" action="contactpage.php" onsubmit="return validateForm()">
            <table>
                <tr>
                    <td>Name:</td>
                    <td><input type="textbox" name="name" id="name" /> *</td>
                </tr>
                <tr>
                    <td>E-mail address:</td>
                    <td><input type="textbox" name="email" id="email" /> *</td>
                </tr>
                <tr>
                    <td>Company:</td>
                    <td><input type="textbox" name="company" id="company" /></asp:TextBox></td>
                </tr>
                <tr>
                    <td>Message:</td>
                    <td><textarea name="message" id="message" rows="10"></textarea> *</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td><input type="submit" id="submit" value="Send form" /> </td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td><em>(* = required)</em></td>
                </tr>
            </table>
        </form>
    </body>
</html>
